package com.example.a2007dou_shop.register

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.a2007dou_shop.R
import com.example.a2007dou_shop.databinding.ActivityRegisterBinding
import com.example.a2007dou_shop.mvvm.BaseActivity
@Route(path = "/app/RegisterActivity")
class RegisterActivity : BaseActivity<ActivityRegisterBinding,RegisterViewModel>() {
    override fun getLayout(): Int = R.layout.activity_register

    override fun getViewModel(): Class<RegisterViewModel>  = RegisterViewModel::class.java


    override fun initView() {
        binding.getCode.setOnClickListener {
            viewModel.sendrcode(binding.etPhone.text.toString())
        }
        binding.btnRegister.setOnClickListener {
            //省略---用户名  密码  手机为空的情况   格式不正确的情况
            //校验验证码
            viewModel.checkrcode(binding.etPhone.text.toString(),binding.etCode.text.toString())
        }

    }

    override fun initData() {
        viewModel.sendSuccess.observe(this){
            ToastUtils.showLong(it.msg)
            if(it.code==0){
                Log.d("sss", "initData: ${it.data}")
                //发送验证码成功，显示在输入框中
                binding.etCode.setText("${it.data}")
            }
        }
        viewModel.sendFail.observe(this){
            ToastUtils.showLong(it)
        }
        viewModel.checkSuccess.observe(this){
            if(it.code==0){
                viewModel.register(binding.etPhone.text.toString(),binding.etName.text.toString(),binding.etPwd.text.toString())
            }else{
                ToastUtils.showLong(it.msg)
            }
        }
        viewModel.registerSuccess.observe(this){
            ToastUtils.showLong(it.msg)
            if(it.code==0){
                //带参数的跳转
               ARouter.getInstance().build("/app/LoginActivity").withString("name",binding.etName.text.toString())
                   .withString("pwd",binding.etPwd.text.toString())
                   .navigation()
            }
        }
    }

}